
$(function () {
    $('#fileTb').datagrid({
        rownumbers: true,//显示行号
        striped: true,
        fit: true,
        singleSelect: true,
        sortOrder: true,
        fitColumns: true,
        pagination: true,
        toolbar: '#toolbar',
        url: ROOT_PATH + '/train/certificate/FileTable.shtml',
        queryParams: {
            sort: 'addTime',
            order: 'desc'
        },
        columns: [[
            {
                field: 'id',hidden:true
            },
            {field: 'realName', title: '姓名', width: 100,formatter: function (value, row, index) {
                return "<a href='javascript:void(0)' class='easyui-linkbutton text-primary' plain='true'   onclick='infoFileData(\""+row.id+"\");'>"+row.realName+"</a>";
            }},
            {field: 'code', title: '编号', width: 100},
            {
                field: 'CompanyName',
                title: '所属部门',
                width: 100,
                formatter: function (value, row, index) {
                    var name = '';
                    if (row.userDepartmentList && row.userDepartmentList.length > 0) {
                        for (var index in row.userDepartmentList) {
                            if (row.userDepartmentList[index].deptName != null) {
                                name += ',' + row.userDepartmentList[index].deptName;
                            }
                        }
                        return name.substring(1);
                    } else {
                        return '';
                    }
                }
            },
            {field: 'isCerId', title: '是否持证', width: 100,formatter: function (value, row, index) {
                var resultV='<span class="color-important text-white" style="padding: 3px;border-radius:5px;">否</span>';
                if (row.certificate &&row.certificate.length>0){
                    for (var index in row.certificate)
                    {
                        var b1=false;var b2=false;
                        if (row.certificate[index].isReview=='1'){
                            if (typeof (row.certificate[index].nextReviewTime) == 'undefined' || row.certificate[index].nextReviewTime == null) {
                                b1=false;
                            }else {
                                var now = new Date();
                                var dateR = new Date(row.certificate[index].nextReviewTime);
                                if (dateR > now){
                                    b1=true;
                                }
                            }
                        }else {
                            b1=true;
                        }
                        if (row.certificate[index].isChanged=='1'){
                            if (typeof (row.certificate[index].nextChangedTime) == 'undefined' || row.certificate[index].nextChangedTime == null) {
                                b2=false;
                            }else {
                                var now = new Date();
                                var dateC = new Date(row.certificate[index].nextChangedTime);
                                if (dateC > now){
                                    b2=true;
                                }
                            }
                        }else {
                            b2=true;
                        }
                        if (b1&&b2){
                            return '<span class="color-success text-white" style="padding: 3px;border-radius:5px;">是</span>';
                        } else {
                            resultV='<span class="color-important text-white" style="padding: 3px;border-radius:5px;">否</span>';
                        }
                    }
                    return resultV;
                }else {
                    return resultV;
                }
                //return row.isCerId==null?'<span class="color-important text-white" style="padding: 3px;border-radius:5px;">否</span>':'<span class="color-success text-white" style="padding: 3px;border-radius:5px;">是</span>';
            }},
            {field: 'pnum', title: '年度培训次数', width: 100},
            {field: 'totalHour', title: '年度培训学时', width: 130},
            {field: 'postName', title: '岗位', width: 100}
        ]]
    });

    $('#btn_advanced_search').on("click", function () {
        $('#queryForm2').toggle();
        $('#fileTb').datagrid('resize');
    });
    $('#btn_clear').on("click", function () {
        $('#queryForm1').form('reset');
        $('#queryForm2').form('reset');
        //$('#yearAnnual').combobox('setValue','').combobox('setText','');
    });

    queryYearthList("yearAnnual");
    queryDepartmentTree();
});
function queryDepartmentTree(){
    $.ajax({
        url:ROOT_PATH + '/permission/department/getDepartmentTree.shtml',//提交地址
        data:{parentId : 0},//将表单数据序列化
        type:"POST",
        dataType:"json",
        success:function(result){
            deptCom('deptIdName',result);
        }
    });
}
function deptCom(id,result) {
    var deptId='#'+id;
    $(deptId).combotree(
        {
            data:result,
            prompt:'请选择',
            require:true,
            loadFilter:function(data){
                var newData = new Array();
                newData.push({id:"",text:'请选择'});
                newData.push(data[0]);
                return newData;
            }
        }
    );
}
function queryComboData(code,queryId) {
    var EMPTY_OPTION = {
        id: '',
        text: '请选择'
    };
    var pageId='#'+queryId;
    $.ajax({
        type: 'post',
        url: ROOT_PATH + '/permission/dictionary/queryDictionaryByDictionaryTypeCode.shtml',
        data: {code: code},
        dataType: "json",
        success: function (result) {
            $(pageId).combobox({
                loadFilter: function (data) {
                    data.unshift(EMPTY_OPTION);
                    return data;
                },
                data: result,
                prompt:'请选择',
                valueField: 'id',
                textField: 'text',
                editable: false,
                panelHeight: '200',
                onSelect: function (record) {
                }
            });
        }
    });
}
/**
 * 重新加载表格
 */
function reLoadData() {
    $('#fileTb').datagrid('clearSelections');
    var queryParams1 = $('#queryForm1').serializeJson();
    var queryParams2 = $('#queryForm2').serializeJson();
    var queryParams = $.extend(queryParams1,queryParams2);
    queryParams["sort"] = 'addTime';
    queryParams["order"] = 'desc';
    $('#fileTb').datagrid('options').queryParams = queryParams;
    $("#fileTb").datagrid('reload', ROOT_PATH + '/train/certificate/FileTable.shtml');
}
function queryYearthList(id){
    var date = new Date();
    var year = date.getFullYear();
    var yearthData = new Array();
    //yearthData.push({id:"",text:"请选择"});
    for(var i =0;i<6;i++){
        var y = {id:year-i,text:(year-i)+'年'};
        yearthData.push(y);
    }
    $('#'+id).combobox(
        {
            data:yearthData,
            prompt:'请选择',
            valueField:'id',
            textField:'text',
            multiple:true,
            editable: false
        }
    );
}
function infoFileData(id) {
    var title = '员工培训档案详情';
    var content = ROOT_PATH + '/train/certificate/fileInfo.shtml?resourceId=' + $.getQueryParam('resourceId')+'&id=' + id;
    openLayers(title, content);
}
function openLayers(title, content) {
    WindowManager.openLayer({
        type: 2,
        resize: false,
        title: title,
        area: ['1150px', '80%'],
        content: content
    });
}


